#import "ViewController.h"
#import <CTSuperPlayer.h>
#import <UIView+MMLayout.h>
#import <Masonry.h>

@interface ViewController () <UIGestureRecognizerDelegate, CTSuperPlayerDelegate>
@property UIView *backView;
@property UIView *playerContainer;
@property CTSuperPlayerView *playerView;
@property CGPoint startPoint;
@property CGPoint originPoint;

@end

@implementation ViewController
{
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.backView = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.backView];
    self.view.backgroundColor = CTSuperPlayerRGBA(0, 0, 0, 1);

    self.playerContainer = [[UIView alloc] init];
    self.playerContainer.backgroundColor = [UIColor blackColor];
    [self.backView addSubview:self.playerContainer];
    self.backView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.playerContainer.mm_fill();

//    // 开始播放
//    [_playerView playWithModel:playerModel];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesWindow:)];
    panRecognizer.delegate = self;
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelaysTouchesBegan:YES];
    [panRecognizer setDelaysTouchesEnded:YES];
    [panRecognizer setCancelsTouchesInView:YES];
    [self.view addGestureRecognizer:panRecognizer];

    self.view.userInteractionEnabled = YES;
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.playerView resetPlayer];
}

- (void)panGesWindow:(UIPanGestureRecognizer *)pan
{
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.startPoint = [pan translationInView:self.view];
        self.originPoint = self.playerContainer.frame.origin;
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        CGPoint point = [pan translationInView:self.view];
        float dx = point.x - self.startPoint.x;
        float dy = point.y - self.startPoint.y;

        int progressIndex = (int)dx;
        int moveIndex = (int)dy;

        CGFloat alpha = 1 - (sqrt(dx * dx + dy * dy) / 500.0);
        if (alpha < 0) alpha = 0;
        if (alpha > 1) alpha = 1;

        self.view.backgroundColor = CTSuperPlayerRGBA(0, 0, 0, alpha);

        self.backView.mm_left(progressIndex).mm_top(moveIndex);
        self.backView.transform = CGAffineTransformScale(CGAffineTransformIdentity, alpha / 2 + 0.5, alpha / 2 + 0.5);
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        CGFloat alpha;
        [self.view.backgroundColor getWhite:nil alpha:&alpha];
        if (alpha < 0.1) {
            [UIView animateWithDuration:0.2 animations:^{
                self.backView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.1, 0.1);
            } completion:^(BOOL finished) {
                [self.playerView resetPlayer];
                [self.view removeFromSuperview];
            }];
        } else {
            [UIView animateWithDuration:0.2 animations:^{
                self.backView.transform = CGAffineTransformIdentity;
                self.backView.mm_left(0).mm_top(0);
                self.view.backgroundColor = CTSuperPlayerRGBA(0, 0, 0, 1);
            }];
        }
    }
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    return YES;
}

- (void)superPlayerBackAction:(CTSuperPlayerView *)player
{
    [self.playerView resetPlayer];
    [self.view removeFromSuperview];
}

@end
